package com.leyou.item.controller;

import com.leyou.item.pojo.Category;
import com.leyou.item.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    //根据父节点查询商品类目
    @GetMapping("list")
    //使用ResponseEntity,成功时返回list集合数据，不成功时返回状态码
    public ResponseEntity<List<Category>>  queryByParantId (
       @RequestParam(value = "pid",defaultValue = "0") Long pid){
        List<Category> categories = this.categoryService.queryListByParant(pid);
        if (categories != null && categories.size() != 0){
            //响应成功，返回200，list集合
            return  ResponseEntity.ok(categories);
        }
        //响应不成功，返回404状态码
        return  ResponseEntity.status(HttpStatus.NOT_FOUND).build();
    }
}
